/************************************************************************
Copyright (c) 2010, 2013, C.R. All rights reserved. 

E-mail: crtoyou@gmail.com; crtoyou@163.com

You can redistribute it and/or modify it under the terms of the
GNU Lesser General Public License as published by the
Free Software Foundation version 3 of the License.

This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. if not, write to the Free Software Foundation, Inc., 
51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
************************************************************************/

#ifndef __CR_PERFORMANCE_TEST_H__
#define __CR_PERFORMANCE_TEST_H__

#include <Windows.h>

class CPerformTest
{
public:
	CPerformTest()
	{
		::QueryPerformanceFrequency(&liFreq);
		::QueryPerformanceCounter(&liBegin);
		::QueryPerformanceCounter(&liEnd);
		llTotal = 0;
		llCount = 0;
	}
	void BeginTime()
	{
		::QueryPerformanceCounter(&liBegin);
	}
	void EndTime()
	{
		::QueryPerformanceCounter(&liEnd);
	}
	LONGLONG TimeDelayMS()
	{
		if (liFreq.QuadPart <= 0)
			return (DWORD)-1;
		LONGLONG delay = (LONGLONG)((double)(liEnd.QuadPart - liBegin.QuadPart) * 1000 / liFreq.QuadPart);
		llTotal += delay;
		llCount ++;
		return delay;
	}
	LONGLONG AverageDelayMS()
	{
		return llCount > 0 ? (LONGLONG)((double)llTotal / llCount) : 0;
	}
private:
	LARGE_INTEGER liFreq, liBegin, liEnd;
	LONGLONG llTotal, llCount;
};


#endif //__CR_PERFORMANCE_TEST_H__